package com.link4a.doms.dao;

import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * @author brown
 */
public abstract class Condition extends AbstractPagination {

	private List<?> ids;

	private String sortProperty;
	private String sortDirection;
	private boolean gbkSortProperty;

	public List<?> getIds() {
		return ids;
	}

	public void setIds(List<?> ids) {
		this.ids = ids;
	}

	public void setSortDirection(String sortDirection) {
		this.sortDirection = sortDirection;
	}

	public String getSortDirection() {
		return StringUtils.defaultIfBlank(sortDirection, getDefaultSortDirection());
	}

	public void setSortProperty(String sortProperty) {
		this.sortProperty = sortProperty;
	}

	public String getSortProperty() {
		return StringUtils.defaultIfBlank(sortProperty, getDefaultSortProperty());
	}

	public String getSort() {
		if (isGbkSortProperty()) {
			return "CONVERT(" + getSortProperty() + " USING gbk) " + getSortDirection();
		} else {
			return getSortProperty() + " " + getSortDirection();
		}
	}

	public String toPattern(String value) {
		return (StringUtils.isBlank(value) ? null : "%" + value.trim().replace("%", "\\%").replace("_", "\\_") + "%");
	}
	
	public String trim(String value) {
		return StringUtils.isBlank(value) ? null : value.trim();
	}

	public abstract String getDefaultSortProperty();

	public String getDefaultSortDirection() {
		return "ASC";
	}

	public boolean isForceIndex() {
		return false;
	}

	public boolean isGbkSortProperty() {
		return gbkSortProperty;
	}

	public void setGbkSortProperty(boolean gbkSortProperty) {
		this.gbkSortProperty = gbkSortProperty;
	}

}
